﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using JX.Core;
using JX.Core.Entity;
using JX.Infrastructure.Common;
using JX.Infrastructure.Data;

namespace JX.Dapper
{
	/// <summary>
	/// 数据库表：Company 的仓储实现类.
	/// </summary>
	public partial class CompanyRepositoryDapper : RepositoryDapper<CompanyEntity>,ICompanyRepositoryDapper
	{
		#region 是否存在扩展数据
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public bool IsExistDataOption(int id, string tableName = "U_CompanyText")
		{
			return IsExistBySql(tableName, "ID=@id", new { id });
		}
		/// <summary>
		/// 是否存在扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="tableName"></param>
		/// <returns></returns>
		public async Task<bool> IsExistDataOptionAsync(int id, string tableName = "U_CompanyText")
		{
			return await IsExistBySqlAsync(tableName, "ID=@id", new { id });
		}
		#endregion

		#region 添加扩展数据
		/// <summary>
		/// 添加扩展数据:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool AddDataOption(string tableName, CompanyEntity entity)
		{
			if (entity.Fields == null)
				return false;
			DataRow[] dataRows = Query.GetDataRows(entity.Fields, "FieldLevel = 1");
			string filedSting = Query.GetFiledSting(dataRows);
			filedSting = filedSting + (string.IsNullOrEmpty(filedSting) ? "ID" : ", ID");
			string parametersString = Query.GetParametersString(dataRows);
			parametersString = parametersString + (string.IsNullOrEmpty(parametersString) ? "@ID" : ", @ID");
			string strSql = Query.GetInsertTableSql(tableName, filedSting, parametersString);
			var cmdParams = GetDynamicParameters(entity.Fields, "FieldLevel = 1");
			cmdParams.Add("ID", entity.CompanyID);
			return ExeSQL(strSql, cmdParams) > 0;
		}
		/// <summary>
		/// 添加扩展数据:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> AddDataOptionAsync(string tableName, CompanyEntity entity)
		{
			if (entity.Fields == null)
				return false;
			DataRow[] dataRows = Query.GetDataRows(entity.Fields, "FieldLevel = 1");
			string filedSting = Query.GetFiledSting(dataRows);
			filedSting = filedSting + (string.IsNullOrEmpty(filedSting) ? "ID" : ", ID");
			string parametersString = Query.GetParametersString(dataRows);
			parametersString = parametersString + (string.IsNullOrEmpty(parametersString) ? "@ID" : ", @ID");
			string strSql = Query.GetInsertTableSql(tableName, filedSting, parametersString);
			var cmdParams = GetDynamicParameters(entity.Fields, "FieldLevel = 1");
			cmdParams.Add("ID", entity.CompanyID);
			return await ExeSQLAsync(strSql, cmdParams) > 0;
		}
		#endregion

		#region 修改扩展数据
		/// <summary>
		/// 修改扩展表数据，不存在则添加:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public bool UpdateDataOption(string tableName, CompanyEntity entity)
		{
			if (IsExistDataOption(entity.CompanyID.ToInt32(), tableName))
			{
				StringBuilder builder = new StringBuilder();
				builder.Append("UPDATE ");
				builder.Append(DataSecurity.FilterBadChar(tableName));
				builder.Append(" SET ");
				DataRow[] rows = Query.GetDataRows(entity.Fields, "FieldLevel = 1");
				if (rows.Length > 0)
				{
					builder.Append(Query.GetUpdateFieldList(rows));
					builder.Append(" WHERE ID = " + entity.CompanyID.ToString());
					return ExeSQL(builder.ToString(), GetDynamicParameters(entity.Fields, "FieldLevel = 1")) > 0;
				}
				return false;
			}
			return AddDataOption(tableName, entity);
		}
		/// <summary>
		/// 修改扩展表数据，不存在则添加:从entity.Fields中获取表列及列值信息
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<bool> UpdateDataOptionAsync(string tableName, CompanyEntity entity)
		{
			if (IsExistDataOption(entity.CompanyID.ToInt32(), tableName))
			{
				StringBuilder builder = new StringBuilder();
				builder.Append("UPDATE ");
				builder.Append(DataSecurity.FilterBadChar(tableName));
				builder.Append(" SET ");
				DataRow[] rows = Query.GetDataRows(entity.Fields, "FieldLevel = 1");
				if (rows.Length > 0)
				{
					builder.Append(Query.GetUpdateFieldList(rows));
					builder.Append(" WHERE ID = " + entity.CompanyID.ToString());
					return ExeSQL(builder.ToString(), GetDynamicParameters(entity.Fields, "FieldLevel = 1")) > 0;
				}
				return false;
			}
			return await AddDataOptionAsync(tableName, entity);
		}
		#endregion

		#region 删除扩展数据
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public bool DeleteDataOption(string tableName, int id)
		{
			string strSQL = "delete from " + tableName + " where id=@id";
			return ExeSQL(strSQL, new { id }) > 0;
		}
		/// <summary>
		/// 删除扩展数据
		/// </summary>
		/// <param name="tableName"></param>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<bool> DeleteDataOptionAsync(string tableName, int id)
		{
			string strSQL = "delete from " + tableName + " where id=@id";
			return await ExeSQLAsync(strSQL, new { id }) > 0;
		}
		#endregion

		#region 得到扩展数据
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetDataOption(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			DataTable dtOption = new DataTable();
			if (id <= 0)
				return dtOption;
			var tableName = GetBySQL<string>("select TableName from Models where ModelType=@ModelType", new { ModelType = ModelType.CompanyOption });
			string strSQL = "select * from " + tableName + " where ID = @ID";
			dtOption = GetDataTableBySql(sql: strSQL, param: new { ID = id }, writeAndRead: writeAndRead);
			return dtOption;
		}
		/// <summary>
		/// 根据主键得到扩展数据
		/// </summary>
		/// <param name="id"></param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public async Task<DataTable> GetDataOptionAsync(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			DataTable dtOption = new DataTable();
			if (id <= 0)
				return dtOption;
			var tableName = GetBySQL<string>("select TableName from Models where ModelType=@ModelType", new { ModelType = ModelType.CompanyOption });
			string strSQL = "select * from " + tableName + " where ID = @ID";
			dtOption = await GetDataTableBySqlAsync(sql: strSQL, param: new { ID = id }, writeAndRead: writeAndRead);
			return dtOption;
		}
		#endregion

		#region 得到公司成员
		/// <summary>
		/// 根据主键得到公司成员
		/// </summary>
		/// <param name="id">公司ID</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetMemberInfo(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			DataTable dtOption = new DataTable();
			if (id <= 0)
				return dtOption;
			string strSQL = "select * from UserView where CompanyID = @ID";
			dtOption = GetDataTableBySql(sql: strSQL, param: new { ID = id }, writeAndRead: writeAndRead);
			return dtOption;
		}
		/// <summary>
		/// 根据主键得到公司成员
		/// </summary>
		/// <param name="id">公司ID</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public async Task<DataTable> GetMemberInfoAsync(int id, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			DataTable dtOption = new DataTable();
			if (id <= 0)
				return dtOption;
			string strSQL = "select * from UserView where CompanyID = @ID";
			dtOption = await GetDataTableBySqlAsync(sql: strSQL, param: new { ID = id }, writeAndRead: writeAndRead);
			return dtOption;
		}
		#endregion
	}
}